﻿using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Class_Builder
{
    public partial class Form1 : Form
    {
        #region Constructors
        public Form1()
        {
            InitializeComponent();
        }
        #endregion

        #region Check Changed
        private void chkWindows_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkWindows.Checked == true)
            {
                this.txtPassword.Enabled = false;
                this.txtUsername.Enabled = false;
            }
            else
            {
                this.txtPassword.Enabled = true;
                this.txtUsername.Enabled = true;
            }
        }
        #endregion

        #region Get Connection
        private string GetConnection(string database = "")
        {
            SqlConnectionStringBuilder connection = new SqlConnectionStringBuilder();
            if (this.comServer.SelectedValue != null)
            {
                connection.DataSource = this.comServer.SelectedValue.ToString();
            }
            else
            {
                connection.DataSource = this.comServer.Text;
            }

            if (!this.chkWindows.Checked)
            {
                connection.UserID = this.txtUsername.Text;
                connection.Password = this.txtPassword.Text;
            }
            else
            {
                connection.IntegratedSecurity = true;
            }

            if (!string.IsNullOrEmpty(database))
            {
                connection.InitialCatalog = database;
            }

            return connection.ToString();
        }
        #endregion

        #region Refresh
        private void button1_Click(object sender, EventArgs e)
        {
            //create connection
            SqlConnection sqlConn = new SqlConnection(GetConnection());

            //open connection
            sqlConn.Open();

            //get databases
            DataTable tblDatabases = sqlConn.GetSchema("Databases");

            //close connection
            sqlConn.Close();

            //add to list
            this.comDatabase.Items.Clear();
            foreach (DataRow row in tblDatabases.Rows)
            {
                String strDatabaseName = row["database_name"].ToString();
                this.comDatabase.Items.Add(strDatabaseName);
            } 
        }
        #endregion

        #region Database Selected
        private void comDatabase_SelectedIndexChanged(object sender, EventArgs e)
        {
            //create connection
            SqlConnection sqlConn = new SqlConnection(GetConnection(this.comDatabase.SelectedItem.ToString()));

            //open connection
            sqlConn.Open();

            //get databases
            DataTable tblDatabases = sqlConn.GetSchema("Tables");

            //close connection
            sqlConn.Close();

            //add to list
            this.comTable.Items.Clear();
            foreach (DataRow row in tblDatabases.Rows)
            {
                String strDatabaseName = row[2].ToString();
                this.comTable.Items.Add(strDatabaseName);
            } 
        }
        #endregion

        #region Table Selected
        private void comTable_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable table = new DataTable();
            table.Columns.Add(new DataColumn("Column Name"));
            table.Columns.Add(new DataColumn("Type", typeof(DataTypes)));
            
            //create connection
            SqlConnection sqlConn = new SqlConnection(GetConnection(this.comDatabase.SelectedItem.ToString()));

            //open connection
            sqlConn.Open();

            SqlCommand cmd = new SqlCommand("SELECT TOP 0 * FROM " + this.comTable.SelectedItem, sqlConn);
            using (var reader = cmd.ExecuteReader())
            {
                //get databases
                DataTable tblDatabases = reader.GetSchemaTable();

                //add to list
                int rowCount = 0;
                foreach (DataColumn row in tblDatabases.Columns)
                {
                    DataRow newRow  = table.NewRow();
                    newRow[0] = row.ColumnName;
                    table.Rows.Add(newRow);
                    rowCount++;
                }
            }

            //close connection
            sqlConn.Close();

            this.dataGridView1.DataSource = table;
        }
        #endregion
    }
}
